find_path(OpenEXR_INCLUDE_DIRS OpenEXR/OpenEXRConfig.h)

file(STRINGS "${OpenEXR_INCLUDE_DIRS}/OpenEXR/OpenEXRConfig.h" OPENEXR_CONFIG_H)

string(REGEX REPLACE "^.*define OPENEXR_VERSION_MAJOR ([0-9]+).*$" "\\1" OpenEXR_VERSION_MAJOR "${OPENEXR_CONFIG_H}")
string(REGEX REPLACE "^.*define OPENEXR_VERSION_MINOR ([0-9]+).*$" "\\1" OpenEXR_VERSION_MINOR "${OPENEXR_CONFIG_H}")
set(OpenEXR_LIB_SUFFIX "${OpenEXR_VERSION_MAJOR}_${OpenEXR_VERSION_MINOR}")

find_library(OpenEXR_LIBRARY IlmImf-${OpenEXR_LIB_SUFFIX})
find_library(OpenEXR_UTIL_LIBRARY IlmImfUtil-${OpenEXR_LIB_SUFFIX})

find_library(OpenEXR_HALF_LIBRARY Half)
find_library(OpenEXR_IEX_LIBRARY Iex-${OpenEXR_LIB_SUFFIX})
find_library(OpenEXR_THREAD_LIBRARY IlmThread-${OpenEXR_LIB_SUFFIX})
find_library(OpenEXR_IEXMATH_LIBRARY IexMath-${OpenEXR_LIB_SUFFIX})
find_library(OpenEXR_MATH_LIBRARY Imath-${OpenEXR_LIB_SUFFIX})

set(OpenEXR_LIBRARIES 
    ${OpenEXR_LIBRARY}
    ${OpenEXR_MATH_LIBRARY}
    ${OpenEXR_IEXMATH_LIBRARY}
    ${OpenEXR_UTIL_LIBRARY} 
    ${OpenEXR_HALF_LIBRARY} 
    ${OpenEXR_IEX_LIBRARY} 
    ${OpenEXR_THREAD_LIBRARY})

include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenEXR REQUIRED_VARS OpenEXR_LIBRARIES OpenEXR_INCLUDE_DIRS)
